<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
      <a-button type="primary" icon="download" @click="handleExportXls('报名信息表')">导出</a-button>
      <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl" @change="handleImportExcel">
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload>
      <!-- 高级查询区域 -->
      <j-super-query :fieldList="superFieldList" ref="superQueryModal" @handleSuperQuery="handleSuperQuery"></j-super-query>
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete"/>删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        class="j-table-force-nowrap"
        @change="handleTableChange">

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="pcaSlot" slot-scope="text">
          <div>{{ getPcaText(text) }}</div>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>

          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="handleDetail(record)">详情</a>
              </a-menu-item>
              <a-menu-item v-if='record.applyStatusId!=1'>
                 <a @click="upStatus(record.id,1);addContract(record.userId,record.applyName,record.applyId);addStu(record.applyId,record.userId)">同意</a>
              </a-menu-item>
              <a-menu-item v-if='record.applyStatusId==0'>
                 <a @click="upStatus(record.id,2)">拒绝</a>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>

      </a-table>
    </div>

    <apply-info-modal ref="modalForm" @ok="modalFormOk"></apply-info-modal>
  </a-card>
</template>

<script>

  import '@/assets/less/TableExpand.less'
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import ApplyInfoModal from './modules/ApplyInfoModal'
  import {filterMultiDictText} from '@/components/dict/JDictSelectUtil'
  import Area from '@/components/_util/Area'
  import {filterDictText, initDictOptions} from "../../../components/dict/JDictSelectUtil";
  import { getAction, postAction } from '../../../api/manage'
  import { FormTypes } from '@/utils/JEditableTableUtil'

  export default {
    name: 'ApplyInfoList',
    mixins:[JeecgListMixin, mixinDevice],
    components: {
      ApplyInfoModal
    },
    data () {
      return {
        description: '报名信息表管理页面',
        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title:'报名编号',
            align:"center",
            dataIndex: 'applyId'
          },
          {
            title:'学生姓名',
            align:"center",
            dataIndex: 'applyName'
          },
          {
            title:'头像',
            align:"center",
            dataIndex: 'applyPhoto',
            scopedSlots: {customRender: 'imgSlot'}
          },
          {
            title:'专业',
            align:"center",
            dataIndex: 'majorId',
            customRender: (text, record, index) => {
              //字典值替换通用方法
              for (let i = 0; i < this.majorListDictOptions.length; i++) {
                if (text==this.majorListDictOptions[i].majorId){
                  return this.majorListDictOptions[i].majorName
                }
              }
            }
          },
          {
            title:'报名时间',
            align:"center",
            dataIndex: 'applyTime'
          },
          {
            title:'手机号',
            align:"center",
            dataIndex: 'applyPhone'
          },
          {
            title:'性别',
            align:"center",
            dataIndex: 'applySex',
            customRender: (text, record, index) => {
              //字典值替换通用方法
              return filterDictText(this.applySexDictOptions, text);
            }
          },
          {
            title:'学历',
            align:"center",
            dataIndex: 'educationId',
            customRender: (text, record, index) => {
              //字典值替换通用方法
              for (let i = 0; i < this.educationListDictOptions.length; i++) {
                if (text==this.educationListDictOptions[i].eduId){
                  return this.educationListDictOptions[i].eduName
                }
              }
            }
          },
          {
            title:'所学专业',
            align:"center",
            dataIndex: 'applyOldMajor'
          },
          {
            title:'电子邮箱',
            align:"center",
            dataIndex: 'applyEmail'
          },
          {
            title:'QQ号码',
            align:"center",
            dataIndex: 'applyQqnumber'
          },
          {
            title:'身份证',
            align:"center",
            dataIndex: 'applyIdcard'
          },
          {
            title:'家庭住址',
            align:"center",
            dataIndex: 'applyAddress',
            scopedSlots: {customRender: 'pcaSlot'}
          },
          {
            title:'监护人关系',
            align:"center",
            type:FormTypes.select,
            dataIndex: 'applyGuardian',
            customRender: (text, record, index) => {
              //字典值替换通用方法
              return filterDictText(this.applyGuardianDictOptions, text);
            }
          },
          {
            title:'监护人电话',
            align:"center",
            dataIndex: 'applyFamilyPhone'
          },
          {
            title:'状态',
            align:"center",
            dataIndex: 'applyStatusId',
            customRender: (text, record, index) => {
              //字典值替换通用方法
              return filterDictText(this.applyStatusDictOptions, text);
            }
          },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            fixed:"right",
            width:147,
            scopedSlots: { customRender: 'action' }
          }
        ],
        url: {
          upApplyStatusUrl:"/applyinfo/applyInfo/upStatus",
          list: "/applyinfo/applyInfo/list",
          delete: "/applyinfo/applyInfo/delete",
          deleteBatch: "/applyinfo/applyInfo/deleteBatch",
          exportXlsUrl: "/applyinfo/applyInfo/exportXls",
          importExcelUrl: "applyinfo/applyInfo/importExcel",
          majorListUrl:"/major/major/majorList",
          educationListUrl:"/education/education/educationList",
          addContractUrl:'/applycontract/applyContract/addContract',
          addStuUrl:'/stu/stu/autoAddStu'
        },
        dictOptions:{},
        pcaData:'',
        applySexDictOptions:[],
        majorListDictOptions:[],
        educationListDictOptions:[],
        applyGuardianDictOptions:[],
        superFieldList:[],
      }
    },
    created() {
      this.initDictConfig();
      this.educationList();
      this.majorList();
      this.pcaData = new Area()
      this.getSuperFieldList();
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      },
    },
    methods: {
      //报名同意后生成合同
      addContract(userId,applyName,applyId){
        postAction(this.url.addContractUrl+'/'+userId+'/'+applyName+'/'+applyId).then((res)=>{
          this.loadData();
        })
      },

      //报名成功后生成一条学生信息
      addStu(applyId,userId){
        postAction(this.url.addStuUrl+'/'+applyId+'/'+userId).then((res)=>{
          this.loadData();
        })
      },
      getPcaText(code){
        return this.pcaData.getText(code);
      },
      //修改状态
      upStatus(id,newApplyStatus){
        this.$http.get(this.url.upApplyStatusUrl+'/'+id+'/'+newApplyStatus).then((res)=>{
          if (res.success){
            this.$message.success('操作成功！');
            this.loadData();//重新加载数据
          }else {
            this.$message.warning('操作失败！');
          }
        })
      },
      //学历翻译
      educationList(){
        getAction(this.url.educationListUrl).then((resp)=>{
          if (resp.success){
            this.educationListDictOptions=resp.result
          }
        })
      },
      //专业翻译
      majorList(){
        getAction(this.url.majorListUrl).then((resp)=>{
          if (resp.success){
            this.majorListDictOptions=resp.result
          }
        })
      },
      //状态翻译
      initDictConfig(){
        //初始化字典
        initDictOptions('apply_status').then((res) => {
          if (res.success) {
            this.applyStatusDictOptions = res.result;
          }
        });
        //性别翻译
        initDictOptions('sex').then((res) => {
          if (res.success) {
            this.applySexDictOptions = res.result;
          }
        });
        //关系翻译
        initDictOptions('apply_guardian').then((res)=>{
          if (res.success){
            this.applyGuardianDictOptions = res.result;
          }
        })
      },
      getSuperFieldList(){
        let fieldList=[];
        fieldList.push({type:'string',value:'applyId',text:'报名编号',dictCode:''})
        fieldList.push({type:'string',value:'applyName',text:'学生姓名',dictCode:''})
        fieldList.push({type:'string',value:'majorId',text:'报名专业',dictCode:"MAJOR,MAJOR_NAME,MAJOR_ID"})
        fieldList.push({type:'datetime',value:'applyTime',text:'报名时间'})
        fieldList.push({type:'string',value:'applyPhone',text:'手机号',dictCode:''})
        fieldList.push({type:'string',value:'applySex',text:'性别',dictCode:''})
        fieldList.push({type:'string',value:'educationId',text:'学历编号',dictCode:"EDUCATION,EDU_NAME,EDU_ID"})
        fieldList.push({type:'string',value:'applyOldMajor',text:'所学专业',dictCode:''})
        fieldList.push({type:'string',value:'applyEmail',text:'电子邮箱',dictCode:''})
        fieldList.push({type:'string',value:'applyQqnumber',text:'QQ号码',dictCode:''})
        fieldList.push({type:'string',value:'applyIdcard',text:'身份证',dictCode:''})
        fieldList.push({type:'pca',value:'applyAddress',text:'家庭住址'})
        fieldList.push({type:'string',value:'applyGuardian',text:'监护人关系',dictCode:''})
        fieldList.push({type:'string',value:'applyFamilyPhone',text:'监护人电话',dictCode:''})
        this.superFieldList = fieldList
      }
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>